﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Forms;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Command;
using GoldDigger.Models;
using GoldDigger.AppServices;

namespace GoldDigger.ViewModels
{
    class SelectDatasetViewModel : ViewModelBase
    {
        private ICommand _selectPedFileCommand;
        private ICommand _selectMapFileCommand;
        private ICommand _importDataCommand;

        private bool _canClick;

        private SourceFileModel _fileModel;
        private MainService _app;

        private delegate void _importParamaters(SourceFileModel param1, bool param2, Action<bool> param3);

        public SelectDatasetViewModel(MainService ms)
        {
            _app = ms;
            _fileModel = new SourceFileModel();
        }

        public ICommand SelectMapFileCommand
        {
            get
            {
                return _selectMapFileCommand ?? (_selectMapFileCommand = new RelayCommand(SelectMapFileDialog));
            }
        }

        public void SelectMapFileDialog()
        {
            OpenFileDialog mapDlg = new OpenFileDialog();
            DialogResult mapDlgResult = mapDlg.ShowDialog();

            if (mapDlgResult == System.Windows.Forms.DialogResult.OK)
            {
                _fileModel.MapFile = mapDlg.FileName;
            }
        }

        public bool CanImport
        {
            get
            {
                return _canClick;
            }

            set
            {
                _canClick = value;
                RaisePropertyChanged("CanImport");
            }
        }

        public ICommand SelectPedFileCommand
        {
            get
            {
                return _selectPedFileCommand ?? (_selectPedFileCommand = new RelayCommand(SelectPedFileDialog));
            }
        }

        public void SelectPedFileDialog()
        {
            OpenFileDialog pedDlg = new OpenFileDialog();
            DialogResult pedDlgResult = pedDlg.ShowDialog();

            if (pedDlgResult == System.Windows.Forms.DialogResult.OK)
            {
                _fileModel.PedFile = pedDlg.FileName;
                CanImport = true;
            }
        }

        public ICommand ImportDataCommand
        {
            get
            {
                return _importDataCommand ?? (_importDataCommand = new RelayCommand(ImportSourceFiles));
            }
        }

        public void ImportSourceFiles()
        {
            Messenger.Default.Send(new NotificationMessage<bool>(true, "Wait"));
            Convert(ImportComplete);
        }

        public void Convert(Action<bool> callback)
        {
            _importParamaters delImport = new _importParamaters(_app.ImportSource);
            delImport.BeginInvoke(_fileModel, false, ImportComplete, null, null);
        }

        public void ImportComplete(bool complete)
        {
            if (complete)
            {
                Messenger.Default.Send(new NotificationMessage<string>("summary", "UpdateMenu"));
                Messenger.Default.Send(new NotificationMessage<bool>(false, "Wait"));
                Messenger.Default.Send(new NotificationMessage<ViewModelBase>(new DataSummaryViewModel(_app), "ChangeVM"));
            }
        }
    }
}
